//index.js
Page({
    data: {
        theme: 'loadingStatus',
        loadingStatus: {
            loading: true
        },
        current: 0,
        itemId: 0,
        switchIndicateStatus: true,
        switchAutoPlayStatus: false,
        switchVerticalStatus: false,
        switchDuration: 500,
        autoPlayInterval: 2000,
        data: [],
        list5: [
            {
            "title": "科技资讯",
            "id": 1,
            },
            {
            "title": "人工智能",
            "id": 3,
            },
            {
            "title": "汽车能源",
            "id": 4,
            }
        ],
    },
    
    onLoad() {
        this.requestTask = new Promise((resolve, reject) => {
            const requestHandler = swan.request({
                url: 'https://host.com/api/index',
                header: {
                    'content-type': 'application/json'
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: res => {
                    this.setData('data', res.data);
                    this.setData({
                        loadingStatus: {
                            loading: false
                        },
                        list5: res.data.menu
                    });
                    resolve();
                },
                fail: err => {
                    // console.log('错误码：' + err.errCode);
                    // console.log('错误信息：' + err.errMsg);
                }
            });
        });
    },
    // 在 onShow 中获取
    onShow() {
        this.requestTask.then( requestData => {
            // 可以直接通过框架提供的 getData 方法获取到 data 字段值; 也可以通过 this.data.xxx 获取
            // let res = this.getData('data');
            swan.setPageInfo({
                // title: res.title,
                title: '唯美资讯',
                keywords: '科技资讯,产品设计,移动互联网,运营文案',
                description: '唯美资讯提供科技资讯、产品设计、移动互联网、运营文案、等聚合信息。',
                articleTitle: '唯美资讯提供科技资讯、产品设计、移动互联网、运营文案！',
                success: res => {
                    // console.log('setPageInfo success');
                },
                fail: err => {
                    // console.log('setPageInfo fail', err);
                }
            })
        })
    },
    onError(errMsg) {
        // 发生错误时，回调此方法并传递错误信息
        // console.log(errMsg); 
    },
    swiperChange(e) {
        this.setData({
            itemId: e.detail.current
        });
    },
    switchIndicate() {
        this.setData({
            switchIndicateStatus: !this.getData('switchIndicateStatus')
        });
    },
    switchVertical() {
        this.setData({
            switchVerticalStatus: !this.getData('switchVerticalStatus')
        });
    },
    switchAutoPlay() {
        this.setData({
            switchAutoPlayStatus: !this.getData('switchAutoPlayStatus')
        });
    },
    changeSwitchDuration(e) {
        this.setData({
            switchDuration: e.detail.value
        });
    },
    changeAutoPlayInterval(e) {
        this.setData({
            autoPlayInterval: e.detail.value
        });
    },
    clickSite(e) {
        // swan.showModal({
        //     title: '请求到的数据',
        //     content: e.currentTarget.dataset.say,
        //     showCancel: false
        // });
        swan.navigateTo({
            url: "/show?id="+e.currentTarget.dataset.say
        });
    },
    clickArticle(e) {
        // swan.showModal({
        //     title: '请求到的数据',
        //     content: e.currentTarget.dataset.say,
        //     showCancel: false
        // });
        swan.navigateTo({
            url: "/show?id="+e.currentTarget.dataset.say
        });
    },
    openSearch() {
        swan.navigateTo({
            url: '/search'
        });
    },
    clickHld({detail}) {
        console.log(detail, '点击项详情');
        swan.navigateTo({
            url: "/list?id="+detail.id
        });
    }
});